﻿using System;
using System.Collections.Generic;
class NumeralConverter
{
    static int InputNumber(int fromValue, int toValue)
    {
        int number = 0;
        do
        {
            if (number != 0)
            {
                Console.Write("number must be from 2 to 16: ");
            }
            while (true)
            {
                try
                {
                    number = int.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("Please re-enter number: ");
                }
            }
        } while ((number < fromValue) || (number > toValue));
        return number;
    }

    static int BinaryToDecimal(string number)
    {
        string temp = "";
        int res = 0;
        int[] binElements = new int[number.Length];
        for (int i = 0; i < number.Length; i++)
        {
            temp = "" + number[i];
            binElements[i] = int.Parse(temp);
        }
        for (int j = 0; j < binElements.Length; j++)
        {
            if ((number[j] != '0') && (number[j] != '1'))
            {
                Console.WriteLine("it is not a BIN number");
                return 0;
            }
            res = res + (binElements[j] * ((int)Math.Pow(2, (binElements.Length - 1 - j))));
        }
        return res;
    }

    static string BinaryToHexadecimal(string biNumber)
    {
        string res = "";
        for (int i = 0; i <= (biNumber.Length % 4); i++)
        {
            biNumber = "0" + biNumber;
        }
        for (int j = 0; j <= (biNumber.Length - 4); j += 4)
        {
            switch (biNumber.Substring(j, 4))
            {
                case "0000":
                    { res = res + "0"; break; }
                case "0001":
                    { res = res + "1"; break; }
                case "0010":
                    { res = res + "2"; break; }
                case "0011":
                    { res = res + "3"; break; }
                case "0100":
                    { res = res + "4"; break; }
                case "0101":
                    { res = res + "5"; break; }
                case "0110":
                    { res = res + "6"; break; }
                case "0111":
                    { res = res + "7"; break; }
                case "1000":
                    { res = res + "8"; break; }
                case "1001":
                    { res = res + "9"; break; }
                case "1010":
                    { res = res + "A"; break; }
                case "1011":
                    { res = res + "B"; break; }
                case "1100":
                    { res = res + "C"; break; }
                case "1101":
                    { res = res + "D"; break; }
                case "1110":
                    { res = res + "E"; break; }
                case "1111":
                    { res = res + "F"; break; }
                default:
                    { break; }
            }
        }
        return res;
    }

    static List<int> DecimalToBinary(int number)
    {
        int index = 0;
        List<int> binElement = new List<int>();
        do
        {
            binElement.Add(number % 2);
            number = number / 2;
            index++;
        } while (number > 0);
        return binElement;
    }

    static string DecimalToHexadecimal(int number)
    {
        string hexNum = "";
        string currentDigit = "";
        string hexNumAns = "";
        do
        {
            if (number % 16 > 9)
            {
                switch (number % 16)
                {
                    case 10:
                        {
                            currentDigit = "A";
                            break;
                        }
                    case 11:
                        {
                            currentDigit = "B";
                            break;
                        }
                    case 12:
                        {
                            currentDigit = "C";
                            break;
                        }
                    case 13:
                        {
                            currentDigit = "D";
                            break;
                        }
                    case 14:
                        {
                            currentDigit = "E";
                            break;
                        }
                    case 15:
                        {
                            currentDigit = "F";
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            else
            {
                currentDigit = "" + number % 16;
            }
            hexNum = hexNum + currentDigit;
            number = number / 16;
        } while (number > 0);
        for (int i = hexNum.Length - 1; i >= 0; i--)
        {
            hexNumAns = hexNumAns + hexNum[i];
        }
        return hexNumAns;
    }

    static string HexadecimalToBinary(string heNumber)
    {
        string result = "";
        for (int i = 0; i < heNumber.Length; i++)
        {
            heNumber.Substring(i, 1);
            switch (heNumber.Substring(i, 1))
            {
                case "0":
                    { result = result + "0000"; break; }
                case "1":
                    { result = result + "0001"; break; }
                case "2":
                    { result = result + "0010"; break; }
                case "3":
                    { result = result + "0011"; break; }
                case "4":
                    { result = result + "0100"; break; }
                case "5":
                    { result = result + "0101"; break; }
                case "6":
                    { result = result + "0110"; break; }
                case "7":
                    { result = result + "0111"; break; }
                case "8":
                    { result = result + "1000"; break; }
                case "9":
                    { result = result + "1001"; break; }
                case "A":
                    { result = result + "1010"; break; }
                case "B":
                    { result = result + "1011"; break; }
                case "C":
                    { result = result + "1100"; break; }
                case "D":
                    { result = result + "1101"; break; }
                case "E":
                    { result = result + "1110"; break; }
                case "F":
                    { result = result + "1111"; break; }
                default:
                    break;
            }
        }
        return result;
    }

    static int HexadecimalToDecimal(string HeNumber)
    {
        int digit = 0;
        int result = 0;
        string temp = "";
        for (int i = 0; i < HeNumber.Length; i++)
        {
            temp = "";
            try
            {
                temp = temp + HeNumber[i];
                digit = int.Parse(temp);
            }
            catch (Exception)
            {
                switch (HeNumber[i])
                {
                    case 'A':
                        {
                            digit = 10;
                            break;
                        }
                    case 'B':
                        {
                            digit = 11;
                            break;
                        }
                    case 'C':
                        {
                            digit = 12;
                            break;
                        }
                    case 'D':
                        {
                            digit = 13;
                            break;
                        }
                    case 'E':
                        {
                            digit = 14;
                            break;
                        }
                    case 'F':
                        {
                            digit = 15;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            result = result + digit * ((int)Math.Pow(16, (HeNumber.Length - 1 - i)));
        }
        return result;
    }

    static void Main()
    {
        Console.Write("please input numeral system from (form 2 to 16): ");
        int fromNumeral = InputNumber(2, 16);
        Console.Write("please input numeral system from (form 2 to 16): ");
        int toNumeral = InputNumber(2, 16);
        if (fromNumeral == 2)
        {
            if (toNumeral == 10)
            {
                Console.Write("input bin number: ");
                string inputBin = Console.ReadLine();
                int result = BinaryToDecimal(inputBin);
                Console.Write("decimal representation of {0}, is: ",inputBin);
                Console.WriteLine(result);
            }
            else if (toNumeral == 16)
            {
                Console.Write("input bin number: ");
                string inputBin = Console.ReadLine();
                string result = BinaryToHexadecimal(inputBin);
                Console.Write("hexadecimal representation of {0}, is: ", inputBin);
                Console.WriteLine(result);
            }
            else if (fromNumeral==toNumeral)
            {
                Console.WriteLine("you entered same numaral systems");
            }
            else
            {
                Console.WriteLine("unknown output numeral system");
            }
        }
        else if (fromNumeral == 10)
        {
            if (toNumeral == 2)
            {
                Console.Write("please input decimal number: ");
                int number = InputNumber(0,int.MaxValue);
                List<int> binDigits = DecimalToBinary(number);
                binDigits.Reverse();
                Console.Write("decimal representation of {0} is: ", number);
                for (int i = 0; i < binDigits.Count; i++)
                {
                    Console.Write(binDigits[i]);
                }
            }
            else if (toNumeral == 16)
            {
                Console.Write("please input decimal number: ");
                int number = InputNumber(0, int.MaxValue);
                string result = DecimalToHexadecimal(number);
                Console.Write("hexadecimal representation of {0} is: ", number);
                Console.WriteLine(result);
            }
            else if (fromNumeral == toNumeral)
            {
                Console.WriteLine("you entered same numaral systems");
            }
            else
            {
                Console.WriteLine("unknown output numeral system");
            }
        }
        else if (fromNumeral == 16)
        {
            if (toNumeral == 2)
            {
                Console.Write("please input Hex number: ");
                string hexNumber = Console.ReadLine();
                string result = HexadecimalToBinary(hexNumber);
                Console.Write("binary representation of {0}, is: ", hexNumber);
                Console.WriteLine(result);
            }
            else if (toNumeral == 10)
            {
                Console.Write("please input Hex number: ");
                string hexNumber = Console.ReadLine();
                int res = HexadecimalToDecimal(hexNumber);
                Console.Write("decimal representation of {0}, is: ", hexNumber);
                Console.WriteLine(res);
            }
            else if (fromNumeral == toNumeral)
            {
                Console.WriteLine("you entered same numaral systems");
            }
            else
            {
                Console.WriteLine("unknown output numeral system");
            }
        }
        else
        {
            Console.WriteLine("unknown input numeral system");
        }
        Console.ReadLine();
    }
}

